package com.dd.demo01.service;

import com.dd.demo01.aop.annotation.TestAop;
import com.dd.demo01.aop.annotation.TwoAop;
import com.dd.demo01.mapper.UserMapper;
import com.dd.demo01.po.User;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author: lkd
 * @date: 2023/04/30/16:54
 * @Description:
 */
@Service
public class TestService {

	@Autowired
	private SqlSessionTemplate sqlSession;
	@Resource
	private UserMapper userMapper;

	@Lazy
	@Resource
	private TestService testService;


	@Transactional
	public User doQuery(Long id) {
		UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
		return userMapper.selectUser(id);
	}

	@Transactional
	public Integer insertUser(User user) {
		int res = userMapper.insertUser(user);
		return res;
	}

	@Transactional(propagation = Propagation.NESTED)
	public Integer updateNameById(Long id, String newName) {
		int res = userMapper.updateNameById(id, newName);
		System.out.println(1/0);
		return res;
	}

	@Transactional
	public void test(User user, Long id, String newName) {
		int res = userMapper.insertUser(user);
		if (res > 0) {
			System.out.println("添加成功");
		}
		try {
			if (testService.updateNameById(id, newName) > 0) {
				System.out.println("修改成功");
			}
		} catch (Exception e) {
		}
	}


	@TestAop
	@TwoAop
	public String testAopReturning() {
		System.out.println("执行完毕");
		return null;
	}


	@TestAop
	@TwoAop
	public String testAopThrowing() {
		System.out.println("异常");
		System.out.println(1 / 0);
		return null;
	}

}
